package cn.com.checne.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import cn.com.checne.model.domain.JobCategory;
import cn.com.checne.model.domain.JobNewsInfo;
import cn.com.checne.model.domain.LanguageInfo;
import cn.com.checne.model.dto.JobNewsQuery;
import cn.com.checne.util.TimeUtils;


@Repository
public class JobNewsInfoDao {
	protected static final Logger log = Logger.getLogger(JobNewsInfoDao.class);

	/**
	 * create
	 * @param cate
	 * @return
	 */
	public boolean create(JobNewsInfo jobNews) {
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String createSql = "insert into tbl_job_news(lg_id_fk,job_news_cate_id_fk,job_news_title,job_news_summary,job_news_publishdate,job_news_publishfrom,job_news_author,job_news_content," +
					"job_news_meta_keyword,job_news_meta_content,job_news_isshowindex,job_news_isshowlist,job_news_istop,job_news_firstimage)" +
					" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			PreparedStatement pstmt = conn.prepareStatement(createSql);
			int i = 0 ;
			pstmt.setInt(++i,jobNews.getLanguageId());
			pstmt.setInt(++i,jobNews.getCateId());
			pstmt.setString(++i,jobNews.getTitle());
			pstmt.setString(++i,jobNews.getSummary());
			pstmt.setTimestamp(++i, new java.sql.Timestamp(jobNews.getPublishDate().getTime()));
			pstmt.setString(++i,jobNews.getPublishFrom());
			pstmt.setString(++i,jobNews.getAuthor());
			pstmt.setString(++i,jobNews.getContent());
			
			pstmt.setString(++i,jobNews.getMetaKeyword());
			pstmt.setString(++i,jobNews.getMetaContent());
			
			pstmt.setInt(++i,jobNews.getIsShowIndex());
			pstmt.setInt(++i,jobNews.getIsShowList());
			pstmt.setInt(++i,jobNews.getIsTop());
			
			pstmt.setString(++i,jobNews.getFirstImage());

			pstmt.execute();
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result;
	}

	/**
	 * @param cate
	 * @return
	 */
	public boolean update(JobNewsInfo jobNews) {
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String updateSql = "update tbl_job_news set " +
					"lg_id_fk = ?,job_news_cate_id_fk = ?,job_news_title = ?,job_news_summary = ?,job_news_publishdate =? ,job_news_publishfrom = ?,job_news_author =?,job_news_content=?," +
					"job_news_meta_keyword =?,job_news_meta_content=?,job_news_isshowindex=?,job_news_isshowlist=?,job_news_istop=?,job_news_firstimage=? where job_news_id = ? ";
			PreparedStatement pstmt = conn.prepareStatement(updateSql);
			int i = 0 ;
			pstmt.setInt(++i,jobNews.getLanguageId());
			pstmt.setInt(++i,jobNews.getCateId());
			pstmt.setString(++i,jobNews.getTitle());
			pstmt.setString(++i,jobNews.getSummary());
			pstmt.setTimestamp(++i, new java.sql.Timestamp(jobNews.getPublishDate().getTime()));
			pstmt.setString(++i,jobNews.getPublishFrom());
			pstmt.setString(++i,jobNews.getAuthor());
			pstmt.setString(++i,jobNews.getContent());
			
			pstmt.setString(++i,jobNews.getMetaKeyword());
			pstmt.setString(++i,jobNews.getMetaContent());
			
			pstmt.setInt(++i,jobNews.getIsShowIndex());
			pstmt.setInt(++i,jobNews.getIsShowList());
			pstmt.setInt(++i,jobNews.getIsTop());
			
			pstmt.setString(++i,jobNews.getFirstImage());
			
			pstmt.setInt(++i, jobNews.getJobNewsId());
			
			pstmt.executeUpdate();
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result;
	}

	/**
	 * @param id
	 * @return
	 */
	public boolean delete(int id){
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String updateSql = "delete from tbl_job_news where job_news_id = ? ";
			PreparedStatement pstmt = conn.prepareStatement(updateSql);
			int i = 0 ;
			pstmt.setInt(++i,id);
			log.info("[SQL]" + pstmt.toString());
			pstmt.executeUpdate();
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result ;
	}
	/**
	 * 
	 * @param JobId
	 * @return
	 */
	public JobNewsInfo getJobById(int jobId) {
		JobNewsInfo job = new JobNewsInfo();
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "select * from tbl_job_news where job_news_id = ? ";

			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, jobId);
			log.info("[SQL]" + pstmt.toString());
			ResultSet rs = pstmt.executeQuery();
			List<JobNewsInfo> list = getResult(rs);
			job = (list != null && list.size() > 0 ) ? list.get(0) : job ;
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return job;
	}
	/**
	 * 
	 * @param query
	 * @param start
	 * @param num
	 * @return
	 */
	public List<JobNewsInfo> getJobNewsListByCondition(JobNewsQuery query,int start,int num) {
		List<JobNewsInfo> list = new ArrayList<JobNewsInfo>();
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "select * from tbl_job_news where 1=1 " ;
			if(query.getLanguageId() > 0){
				sql += " and lg_id_fk = ? " ;
			}
			if(query.getCateId() > 0){
				sql += " and job_news_cate_id_fk = ? " ;
			}
			if(query.getIsShowIndex() > -1){
				sql += " and job_news_isshowindex = ? " ;
			}
			if(query.getIsShowList() > -1){
				sql += " and job_news_isshowlist = ? " ;
			}
			if(query.getIsTop() > -1){
				sql += " and job_news_istop = ? " ;
			}
			if(StringUtils.isNotBlank(query.getKw())){
				sql += " and (job_news_title like ? or job_news_meta_keyword like ? or job_news_meta_content like ? or job_news_content like ? )";
			}
			if(StringUtils.isNotBlank(query.getStartTime())){
				sql += " and job_news_publishdate >= ? ";
			}
			if(StringUtils.isNotBlank(query.getEndTime())){
				sql += " and job_news_publishdate <= ? ";
			}
			sql += "order by job_news_publishdate desc limit ?,?";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			int i = 0 ;
			if(query.getLanguageId() > 0){
				pstmt.setInt(++i, query.getLanguageId());
			}
			if(query.getCateId() > 0){
				pstmt.setInt(++i, query.getCateId());
			}
			if(query.getIsShowIndex() > -1){
				pstmt.setInt(++i, query.getIsShowIndex());
			}
			if(query.getIsShowList() > -1){
				pstmt.setInt(++i, query.getIsShowList());
			}
			if(query.getIsTop() > -1){
				pstmt.setInt(++i, query.getIsTop());
			}
			if(StringUtils.isNotBlank(query.getKw())){
				pstmt.setString(++i, "%" + query.getKw() + "%");
				pstmt.setString(++i, "%" + query.getKw() + "%");
				pstmt.setString(++i, "%" + query.getKw() + "%");
				pstmt.setString(++i, "%" + query.getKw() + "%");
			}
			if(StringUtils.isNotBlank(query.getStartTime())){
				pstmt.setTimestamp(++i, new java.sql.Timestamp(TimeUtils.formatDate(query.getStartTime()).getTime()));
			}
			if(StringUtils.isNotBlank(query.getEndTime())){
				pstmt.setTimestamp(++i, new java.sql.Timestamp(TimeUtils.formatDate(query.getEndTime()).getTime()));
			}
			pstmt.setInt(++i, start);
			pstmt.setInt(++i, num);
			log.info("[SQL]" + pstmt.toString());
			ResultSet rs = pstmt.executeQuery();
			list.addAll(getResult(rs));
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 
	 * @param query
	 * @return
	 */
	public int getJobNewsCountByCondition(JobNewsQuery query) {
		int total = 0 ;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "select count(1) as total from tbl_job_news where 1=1 " ;
			if(query.getLanguageId() > 0){
				sql += " and lg_id_fk = ? " ;
			}
			if(query.getCateId() > 0){
				sql += " and job_news_cate_id_fk = ? " ;
			}
			if(query.getIsShowIndex() > -1){
				sql += " and job_news_isshowindex = ? " ;
			}
			if(query.getIsShowList() > -1){
				sql += " and job_news_isshowlist = ? " ;
			}
			if(query.getIsTop() > -1){
				sql += " and job_news_istop = ? " ;
			}
			if(StringUtils.isNotBlank(query.getKw())){
				sql += " and (job_news_title like ? or job_news_meta_keyword like ? or job_news_meta_content like ? or job_news_content like ? )";
			}
			if(StringUtils.isNotBlank(query.getStartTime())){
				sql += " and job_news_publishdate >= ? ";
			}
			if(StringUtils.isNotBlank(query.getEndTime())){
				sql += " and job_news_publishdate <= ? ";
			}
			PreparedStatement pstmt = conn.prepareStatement(sql);
			int i = 0 ;
			if(query.getLanguageId() > 0){
				pstmt.setInt(++i, query.getLanguageId());
			}
			if(query.getCateId() > 0){
				pstmt.setInt(++i, query.getCateId());
			}
			if(query.getIsShowIndex() > -1){
				pstmt.setInt(++i, query.getIsShowIndex());
			}
			if(query.getIsShowList() > -1){
				pstmt.setInt(++i, query.getIsShowList());
			}
			if(query.getIsTop() > -1){
				pstmt.setInt(++i, query.getIsTop());
			}
			if(StringUtils.isNotBlank(query.getKw())){
				pstmt.setString(++i, "%" + query.getKw() + "%");
				pstmt.setString(++i, "%" + query.getKw() + "%");
				pstmt.setString(++i, "%" + query.getKw() + "%");
				pstmt.setString(++i, "%" + query.getKw() + "%");
			}
			if(StringUtils.isNotBlank(query.getStartTime())){
				pstmt.setTimestamp(++i, new java.sql.Timestamp(TimeUtils.formatDate(query.getStartTime()).getTime()));
			}
			if(StringUtils.isNotBlank(query.getEndTime())){
				pstmt.setTimestamp(++i, new java.sql.Timestamp(TimeUtils.formatDate(query.getEndTime()).getTime()));
			}
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				total = rs.getInt("total");
			}
			rs.close();
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return total;
	}
	
	
	public boolean setShowIndex(int flag,int...jobIds){
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String updateSql = "update tbl_job_news set job_news_isshowindex = ? where job_news_id = ? ";
			PreparedStatement pstmt = conn.prepareStatement(updateSql);
			for(int jobId : jobIds){
				int i = 0 ;
				pstmt.setInt(++i,flag);
				pstmt.setInt(++i,jobId);
				pstmt.addBatch();
			}
			pstmt.executeBatch();
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result ;
	}
	
	public boolean setShowList(int flag,int...jobIds){
		boolean result = false;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String updateSql = "update tbl_job_news set job_news_isshowlist = ? where job_news_id = ? ";
			PreparedStatement pstmt = conn.prepareStatement(updateSql);
			for(int jobId : jobIds){
				int i = 0 ;
				pstmt.setInt(++i,flag);
				pstmt.setInt(++i,jobId);
				pstmt.addBatch();
			}
			pstmt.executeBatch();
			conn.close();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false ;
		}
		return result ;
	}
	
	public JobNewsInfo isJobNewsExist(int languageId ,int cateId){
		JobNewsInfo job = null ;
		try {
			Connection conn = DBFactory.getInstance().getConnection();
			String sql = "select * from tbl_job_news where lg_id_fk = ? and job_news_cate_id_fk = ?   " ;
			PreparedStatement pstmt = conn.prepareStatement(sql);
			int i = 0 ;
			pstmt.setInt(++i, languageId);
			pstmt.setInt(++i, cateId);
			log.info("[SQL]" + pstmt.toString());
			ResultSet rs = pstmt.executeQuery();
			List<JobNewsInfo> list = getResult(rs);
			job = (list != null && list.size() > 0 ) ? list.get(0) : null;
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return job ;
	}
	/**
	 * get rseult from resultSet
	 * @param rs
	 * @return
	 */
	public List<JobNewsInfo> getResult(ResultSet rs) {
		List<JobNewsInfo> list = new ArrayList<JobNewsInfo>();
		try {
			JobNewsInfo jobNews = null ;
			while(rs.next()) {
				jobNews = new JobNewsInfo();
				jobNews.setJobNewsId(rs.getInt("job_news_id"));
				jobNews.setTitle(rs.getString("job_news_title"));
				jobNews.setSummary(rs.getString("job_news_summary"));
				jobNews.setPublishDate(rs.getTimestamp("job_news_publishdate"));
				jobNews.setPublishFrom(rs.getString("job_news_publishfrom"));
				jobNews.setAuthor(rs.getString("job_news_author"));
				jobNews.setContent(rs.getString("job_news_content"));
				jobNews.setMetaKeyword(rs.getString("job_news_meta_keyword"));
				jobNews.setMetaContent(rs.getString("job_news_meta_content"));
				jobNews.setIsShowIndex(rs.getInt("job_news_isshowindex"));
				jobNews.setIsShowList(rs.getInt("job_news_isshowlist"));
				jobNews.setIsTop(rs.getInt("job_news_istop"));
				jobNews.setFirstImage(rs.getString("job_news_firstimage"));
				
				jobNews.setLanguageId(rs.getInt("lg_id_fk"));
				LanguageInfo lg = new LanguageInfoDao().getLanguageInfoById(jobNews.getLanguageId());
				jobNews.setLanguageInfo(lg);
				
				jobNews.setCateId(rs.getInt("job_news_cate_id_fk"));
				JobCategory cate = new JobCategoryDao().getJobCategory(jobNews.getCateId());
				jobNews.setJobCategory(cate);
				
				list.add(jobNews);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}